package com.base.cn.platform.os.controller.op.coupon;

import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.entity.op.coupon.OpCoupon;
import com.base.cn.platform.os.entity.op.coupon.OpCouponShop;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponCodeService;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponService;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优惠券接口
 *
 * @author s.li
 * @create 2018-08-30-11:55
 */
@RestController
@RequestMapping("/api/op")
public class ApiOpCouponController extends ApiBaseController {

    @Autowired
    private OpCouponCodeService couponCodeService;
    @Autowired
    private OpCouponService opCouponService;
    @Autowired
    private OpCouponShopService opCouponShopService;

    /**
     * 我的优惠券列表
     * @param userId 用户ID
     * @param currentPage 页码
     * @return  Map<String,Object>
     */
    @RequestMapping("/couponList")
    public Map<String,Object> couponList(@RequestParam("userId") BigDecimal userId,
                                         @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String, Object> couponCode = new HashMap<>();
        couponCode.put("userId",userId);
        Map<String, Object> couponCodeListPage = couponCodeService.queryOpCouponCodeListPage(couponCode, currentPage, false, true);
        return ResultUtil.SUCCESS(couponCodeListPage);
    }

    /**
     * 获取优惠券关联的商品数据
     * @param couponId 优惠券ID
     * @return Map<String,Object>
     */
    @RequestMapping("/showCouponShopData")
    @ResponseBody
    public Map<String,Object> showCouponShopData(@RequestParam("couponId") BigDecimal couponId){
        List<OpCouponShop> couponShopList = opCouponShopService.showCouponShopData(couponId);
        return ResultUtil.SUCCESS(couponShopList);
    }

    /**
     * 获取课程可以领取的优惠券数据列表
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return Map<String,Object>
     */
    @RequestMapping("/getCourseCouponList")
    public Map<String,Object> getCourseCouponList(@RequestParam(value = "userId",required = false,defaultValue = "0") BigDecimal userId,
                                                  @RequestParam("courseId") BigDecimal courseId){
        List<OpCoupon> couponList = opCouponService.getCourseCouponList(courseId,userId);
        return ResultUtil.SUCCESS(couponList);
    }

    /**
     * 用户领取优惠券
     * @param userId 用户ID
     * @param couponIds 优惠券ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/couponReceive")
    @ResponseBody
    public Map<String,Object> couponReceive(@RequestParam(value = "userId") BigDecimal userId,
                                            @RequestParam("couponIds") String couponIds){
        //获取用户ID
        return opCouponService.couponReceive(couponIds,userId);
    }

    /**
     * 获取针对部分用户可以领取的优惠数据列表
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/findForUsersCouponList")
    public Map<String,Object> findForUsersCouponList(@RequestParam(value = "userId") BigDecimal userId){
        return opCouponService.findForUsersCouponList(userId);
    }
}
